/**
 * File    : MessageFormatBinding.java
 * Created : 2008-10-19
 * By      : LuHuiguo
 */
package com.luhuiguo.tapestry.bindings;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.Binding;
import org.apache.tapestry5.internal.bindings.AbstractBinding;
import org.apache.tapestry5.ioc.Location;
import org.apache.tapestry5.ioc.Messages;

/**
 * @author LuHuiguo
 * @version $Id: MessageFormatBinding.java 16 2008-10-19 02:07:36Z LuHuiguo $
 * 
 */
public class MessageFormatBinding extends AbstractBinding {
	private final String messageKey;
	private final Messages messages;
	private final List<Binding> bindings;
	private final boolean invariant;

	public MessageFormatBinding(Location location, String messageKey,
			Messages messages, List<Binding> bindings, boolean invariant) {
		super(location);

		this.messageKey = messageKey;
		this.messages = messages;
		this.bindings = bindings;
		this.invariant = invariant;
	}

	public Object get() {
		List<Object> values = new ArrayList<Object>(bindings.size());
		for (Binding binding : bindings)
			values.add(binding.get());

		return messages.format(messageKey, values.toArray());
	}

	@Override
	public boolean isInvariant() {
		return this.invariant;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Class getBindingType() {
		return String.class;
	}

}
